package controller

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"gowork/dynamicCheckSheet/model"
	"strconv"
)

type DynamicController struct {
	Ctx  iris.Context
	CheckNode *model.CheckNode
	Paper *model.Paper
	Order *model.Order
	User *model.User
	Region *model.Region
}

func (dc *DynamicController)PostInsertchecknode() mvc.Result {

	dc.CheckNode = new(model.CheckNode)

	regionIdStr := dc.Ctx.FormValue("regionId")
	regionId, err := strconv.ParseInt(regionIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	checkNodeName := dc.Ctx.FormValue("checkNodeName")

	roleIdStr := dc.Ctx.FormValue("roleId")
	roleId, err := strconv.ParseInt(roleIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	nextCheckNodeIdStr := dc.Ctx.FormValue("nextCheckNodeId")
	nextCheckNodeId, err := strconv.ParseInt(nextCheckNodeIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := dc.CheckNode.InsertCheckNode(regionId, checkNodeName, roleId, nextCheckNodeId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (dc *DynamicController)PostDeletechecknode() mvc.Result {

	dc.CheckNode = new(model.CheckNode)

	checkNodeIdStr := dc.Ctx.FormValue("checkNodeId")
	checkNodeId, err := strconv.ParseInt(checkNodeIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := dc.CheckNode.DeleteCheckNode(checkNodeId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (dc *DynamicController)PostConfirmation() mvc.Result {

	dc.Order = new(model.Order)

	orderIdStr := dc.Ctx.FormValue("orderId")
	orderId, err := strconv.ParseInt(orderIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := dc.Order.Confirmation(orderId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (dc *DynamicController)PostExamination() mvc.Result {

	dc.Paper = new(model.Paper)

	var paper model.Paper
	dc.Ctx.ReadJSON(&paper)

	res, err := dc.Paper.Examination(&paper)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (dc *DynamicController)PostQueryrolepaper() mvc.Result {

	dc.Paper = new(model.Paper)

	roleIdStr := dc.Ctx.FormValue("roleId")
	roleId, err := strconv.ParseInt(roleIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := dc.Paper.QueryRolePaper(roleId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}

func (dc *DynamicController)PostDeleteuser() mvc.Result {

	dc.User = new(model.User)

	userIdStr := dc.Ctx.FormValue("userId")
	userId, err := strconv.ParseInt(userIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := dc.User.DeleteUser(userId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (dc *DynamicController)PostDeleteregion() mvc.Result {

	dc.Region = new(model.Region)

	regionIdStr := dc.Ctx.FormValue("regionId")
	regionId, err := strconv.ParseInt(regionIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := dc.Region.DeleteRegion(regionId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (dc *DynamicController)PostQuerychecknodelink() mvc.Result {

	dc.CheckNode = new(model.CheckNode)

	regionIdStr := dc.Ctx.FormValue("regionId")
	regionId, err := strconv.ParseInt(regionIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := dc.CheckNode.QueryCheckNodeLinkFirst(regionId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}

func (dc *DynamicController)PostQueryuserpaper() mvc.Result {

	dc.Paper = new(model.Paper)

	userIdStr := dc.Ctx.FormValue("userId")
	userId, err := strconv.ParseInt(userIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := dc.Paper.QueryPaperListByUserId(userId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}

func (dc *DynamicController)PostInsertorderasync() mvc.Result {

	dc.Order = new(model.Order)

	var order model.Order
	dc.Ctx.ReadJSON(&order)

	res, err := dc.Order.InsertOrderAsync(&order)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}








































